import axios from 'axios'

// respone拦截器
axios.interceptors.response.use(response => {
  return response.data
}, error => {
  return error
})
axios.defaults.withCredentials = true


function timestampToTime(timestamp) {
  let date = new Date(timestamp),
    Y = date.getFullYear() + '-',
    M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-',
    D = date.getDate() + ' ',
    h = date.getHours() + ':',
    m = date.getMinutes() + ':',
    s = date.getSeconds()
  return Y + M + D + h + m + s
}

export default {
  post(url, data, ...args) {
    let argsObj = {};
    args.forEach(item => {
      console.log(item)
      argsObj = {
        ...argsObj,
        ...item
      }
    })
    var Authorization = sessionStorage['token'];
    return axios({
      method: 'post',
      url,
      data,
      timeout: 600000,
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Authorization': Authorization,
        'IP': '',
      },
      withCredentials: false,
      ...argsObj
    }).then(
      (response) => {
        if (response == undefined) {
          return response;
        }
        // console.log(response);
        //0为错误，需要显示错误信息。1为正常结果，处理所需逻辑
        let code = response.code;
        if (code == 0) {
          //此处需要使用更友好的展示方式
          this.$message.error(response.msg);
          return response;
        }
        return response
      }
    ).catch(response => {
      console.log('err', response);
      loading.close();
      loading.close();
      this.$message({
        showClose: true,
        message: response.message,
        type: "error"
      });
    })
  },

  upload(url, data) {
    var Authorization = sessionStorage['token'];
    let form = new FormData()
    Object.keys(data).forEach(value => {
      form.append(value, data[value])
    })
    return axios({
      method: 'post',
      // baseURL: process.env.BASE_API,
      url,
      data,
      transformRequest: [function (data) {
        let ret = ''
        for (let it in data) {
          ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
        }
        return ret
      }],
      timeout: 300000,
      headers: {
        // 'X-Requested-With': 'XMLHttpRequest',
        'Content-Type': 'application/x-www-form-urlencoded',
        'Authorization': Authorization
      }
    }).then(
      (response) => {
        return response
      }
    ).then(
      (res) => {
        return res
      }
    )
  },

  get(url, params) {
    return axios({
      method: 'get',
      // baseURL: process.env.BASE_API,
      url,
      params, // get 请求时带的参数
      timeout: 15000
    }).then(
      (response) => {
        return response
      }
    ).then(
      (res) => {
        return res
      }
    )
  },
  put(url, data, params, ...args) {
    let argsObj = {};
    args.forEach(item => {
      console.log(item)
      argsObj = {
        ...argsObj,
        ...item
      }
    })
    return axios({
        method: "PUT",
        // baseURL: process.env.BASE_API,
        url,
        data,
        headers: {
          // "Content-Type": "application/x-www-form-urlencoded",
          "Content-Type": "file.type || ''",
          "x-cos-security-token": params.XCosSecurityToken,
          "Authorization": params.auth,
        },
        withCredentials: false,
        timeout: 600000,
        ...argsObj
      })
      .then((response) => {
        return response;
      })
      .then((res) => {
        return res;
      });
  },
  time(tamp) {
    return timestampToTime(tamp)
  }
}
